package com.wave12.pro_cart.task;
import com.wave12.pro_cart.entity.ProCart;
import com.wave12.pro_cart.service.ProCartService;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Component
public class GetCartScheduleTask {
    
    
    @Autowired
    private ProCartService proCartService;
    
    //@Scheduled(cron="0/20 * * * * ? ")  // 每隔10分钟执行一次
    public void getCouponFromQuanma() throws IOException {
        
        String url = "https://www.quanyoubuy.com/index/cate/cid/1.html";
    
        String div = Jsoup.connect(url).get().html();
        // 获取产品 所有
        int benfor = div.indexOf("<ul class=\"goods-list clear\" style=\"width:1200px;overflow: hidden;\">");
        int end = div.indexOf("</div></li>\n" +
            "    </ul>");
        String substring = div.substring(benfor, end);
        // end 获取产品 所有
    
        int getProBe = substring.indexOf("<div class=\"good-pic\">");
        int getProEnd = substring.indexOf("<div class=\"item-btn-box \">");
        String substring1 = substring.substring(getProBe , getProEnd);
        // -- 获取单个产品 end
    
        // 解析商品详情
    
        int getProId = substring1.indexOf("/item/");
        int getProIdEnd = substring1.indexOf("\" class=\"pic-img\"");
        String projectId = substring1.substring(getProId , getProIdEnd); // 获得商品信息userId
        String httpUserId = "https://www.quanyoubuy.com";
        System.out.println("projectId="+httpUserId+projectId);
        int getProName = substring1.indexOf("<img alt=\"");
        int getProNameEnd = substring1.indexOf("\" class=\"lazy good-pic\"");
        String proName = substring1.substring(getProName+10, getProNameEnd); // 获得商品信息名称
        System.out.println("proName="+proName);
    
        int getProImg = substring1.indexOf("data-original=\"");
        int getProImgEnd = substring1.indexOf("\"></a>\n" +
            "       </div>");
        String img = substring1.substring(getProImg+14, getProImgEnd); // 获得商品信息 图片
    
        int getProPriceCurrent = substring1.indexOf("<span class=\"price-current\"><em>￥</em>");
        int desOther = substring1.indexOf("</span><span class=\"des-other\">");
        String PriceCurrent = substring1.substring(getProPriceCurrent+38, desOther);// 获得商品信息  券后
    
        int getProPriceold = substring1.indexOf("<span class=\"price-old\"><em>￥</em>");
        int getProPriceoldEnd = substring1.indexOf("</span></span><span class=\"sold\">");
        String ProPriceold = substring1.substring(getProPriceold+34, getProPriceoldEnd);// 获得商品信息  原价
    
        for (int i = 0 ;i<substring.length();i++) {
            if (substring.length()<1840) {
                break;
            } else {
                getSubString(substring.length(), substring);
            }
            

        }
    }

    //
    
    /**
     * 获取新的字符串长度
     * @param i
     * @param str
     * @return
     */
    public String getSubString(int i,String str){
        ProCart proCart = new ProCart();
        int buyNum = str.indexOf("<li style=\"box-sizing:border-box;\"");
        int li = str.indexOf("</div></li>");
        int ii = 0;
        int proTypePre = str.indexOf("g_type=\"");
    
        String proType = str.substring(proTypePre+8, proTypePre+9); // 获得商品类型
        
        while (i>buyNum+li*ii){
           
            StringBuilder sb = new StringBuilder(str);
            StringBuilder delete = sb.delete(buyNum, li+17);
    
            // 解析商品详情
    
            int getProId = delete.indexOf("/item/");
            int getProIdEnd = delete.indexOf("\" class=\"pic-img\"");
            String projectId = delete.substring(getProId , getProIdEnd); // 获得商品信息userId
            String httpUserId = "https://www.quanyoubuy.com/";
            int getProName = delete.indexOf("<img alt=\"");
            int getProNameEnd = delete.indexOf("\" class=\"lazy good-pic\"");
            String proName = delete.substring(getProName+10, getProNameEnd); // 获得商品信息名称
    
            int getProImg = delete.indexOf("data-original=\"");
            int getProImgEnd = delete.indexOf("\"></a>\n" +
                "       </div>");
            String img = delete.substring(getProImg+15, getProImgEnd); // 获得商品信息 图片
            if (img.length()>=104) {
                img = img.substring(0,104);
            }
    
            int getProPriceCurrent = delete.indexOf("<span class=\"price-current\"><em>￥</em>");
            int desOther = delete.indexOf("</span><span class=\"des-other\">");
            String PriceCurrent = delete.substring(getProPriceCurrent+38, desOther);// 获得商品信息  券后
            int i1 = PriceCurrent.indexOf("<i>");
            String PriceCurrentNum = null;
            if (i1>1){
                i1 = PriceCurrent.indexOf("<i>");
                int i2 = PriceCurrent.indexOf("</i>");
                PriceCurrentNum = PriceCurrent.substring(i1 + 3, i2);
                String PriceCurrentPre = PriceCurrent.substring(0, i1);
                PriceCurrent = PriceCurrentPre+PriceCurrentNum;
                String[] split = StringUtils.split(PriceCurrent, ".");
                String s1 = null;
                String s2 = null;
                String s = null;
                for (int j = 0 ;j<split.length;j++) {
                    s = split[0];
                    s2 = split[1];
                    if (s2.length() == 1){
                        s2 = s2 + "0";
                        PriceCurrent = s+s2;
                    }
                }
                proCart.setCouponVal(Integer.parseInt(PriceCurrent));
            } else {
                proCart.setCouponVal(Integer.parseInt(PriceCurrent)*100); // 优惠券券值
            }
    
            int getProPriceold = delete.indexOf("<span class=\"price-old\"><em>￥</em>");
            int getProPriceoldEnd = delete.indexOf("</span></span><span class=\"sold\">");
            String ProPriceold = delete.substring(getProPriceold+34, getProPriceoldEnd);// 获得商品信息  原价
            String[] split1 = StringUtils.split(ProPriceold, ".");
            if (split1.length>1) {
                String s1 = null;
                String s2 = null;
                String s = null;
                for (int j = 0 ;j<split1.length;j++) {
                    s = split1[0];
                    s2 = split1[1];
                    if (s2.length() == 1){
                        s2 = s2 + "0";
                        ProPriceold = s+s2;
                    }
                }
                proCart.setPriceSrc(Integer.parseInt(ProPriceold));
            } else {
                proCart.setPriceSrc(Integer.parseInt(ProPriceold)*100);
            }
    
            int getProSold = delete.indexOf("<span class=\"sold\"><em>");
            int getProSoldEnd = delete.indexOf("</em>人已购买");
            String proSold = delete.substring(getProSold+23, getProSoldEnd);// 人已购买
    
            // end
            
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            proCart.setId(uuid);
            proCart.setUserId("1");
            proCart.setType(0);
            proCart.setCartName(proName);
            proCart.setMainPic(img);
            proCart.setOrderUrl(httpUserId+projectId);
    
            proCart.setDtPublish(new Date());
            proCart.setCateId(proType);
            proCart.setGetNums(Integer.parseInt(proSold));
            proCartService.inertProjectDetail(proCart);
            if (li==-1) {
                return null;
            }else {
                // 迭代获取数据
                return getSubString(i,delete.toString());
            }
           
        }
        return null;
    }
    
    public static void main(String[] args) {
        String str = "123";
        String str1 = "23";
        /*double v = Double.parseDouble(str);
        double v1 = Double.parseDouble(str1);
        double v2 = v - v1;*/
        double2String(str,str1);
        System.out.println( double2String(str,str1));
    }
    
    public static String double2String(String str,String str1){
        double v = Double.parseDouble(str);
        double v1 = Double.parseDouble(str1);
        double v2 = v - v1;
        String s =v2+"";
        String substring = s.substring(0, s.length() - 2);
        System.out.println(substring);
        return v2+"";
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

}
